//
//  ImageLoard.m
//  Test
//
//  Created by sunx on 15/3/17.
//  Copyright (c) 2015年 sunx. All rights reserved.
//

#import "ImageLoard.h"
#import "NSURLSession+KKAddition.h"

@implementation ImageLoader
+ (void)loardImageToLocation:(UIImageView *)imageView imageURL:(NSString *)url andFile:(NSString *)fileType {
    NSArray *array = [url componentsSeparatedByString:@"/"];
    NSString *imageName = [array lastObject];
    NSString *save = [[NSString alloc] initWithFormat:@"%@/%@/",NSHomeDirectory(),fileType];
    NSString *path = [[NSString alloc] initWithFormat:@"%@%@",save,imageName];
    if(![ImageLoader isExitLacation:path]) {
        [NSURLSession Download:url saveTo:save progress:^(CGFloat progress) {
            
        } completed:^(NSString *image){
            
            NSLog(@"conpeleted...");
            
        } failed:^(NSError *error) {
            
            NSLog(@"%@",error);
            
        }];
    }
    NSLog(@"%@",path);
    UIImage *img = [[UIImage alloc] initWithContentsOfFile:path];
    imageView.image = img;
}
+(BOOL)isExitLacation:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:path]) {
        return YES;
    }
    return NO;
}

+ (void)loardImageToDocuments:(UIImageView *)imageView imageURL:(NSString *)url {
    [ImageLoader loardImageToLocation:imageView imageURL:url andFile:@"Documents"];
}
+ (void)loardImageToLibrary:(UIImageView *)imageView imageURL:(NSString *)url {
    [ImageLoader loardImageToLocation:imageView imageURL:url andFile:@"Library"];
}
+ (void)loardImageToTMP:(UIImageView *)imageView imageURL:(NSString *)url {
    [ImageLoader loardImageToLocation:imageView imageURL:url andFile:@"tmp"];
}


@end
